import { createStore } from 'vuex'
import api from '@/api/index.js'
export default createStore({
  state: {
    goods: [],//播放列表
    id: 2187720, //歌曲id
    lyric: '', // 歌词
    playIndex: 0, //播放下标
    name: '', // 歌曲名
    songname: '', // 歌手名
    ablumname: '', // 专辑名
    picUrl: '', //歌曲图片
    currentTime: 0,
  },
  getters: {
    lyriclist: function (state) {
      let arr = state.lyric.split(/\n/gis).map((item) => {
        let min = parseInt(item.slice(1, 3))
        let sec = parseInt(item.slice(4, 6))
        let mill = parseInt(item.slice(7, item.indexOf(']')))
        return {
          min, sec, mill,
          lyric: item.slice(item.indexOf(']') + 1, item.length),
          content: item,
          pre: mill + sec * 1000 + min * 60 * 1000
        }
      })
      arr.forEach((item, i) => {
        if (i + 1 >= arr.length - 1) {
          item.time = 999999
        } else {
          item.time = arr[i + 1].pre
        }
      })
      arr.splice(arr.length - 1, 1)
      return arr
    }
  },
  mutations: {
    setGoods(state, value) {
      state.goods = value
    },
    setId(state, value) {
      state.id = value
    },
    setLyric(state, value) {
      state.lyric = value
    },
    setPlayIndex(state, value) {
      state.playIndex = value
    },
    setName(state, value) {
      state.name = value
    },
    setSongName(state, value) {
      state.songname = value
    },
    setAblumName(state, value) {
      state.ablumname = value
    },
    setPicUrl(state, value) {
      state.picUrl = value
    },
    setCurrentTime(state, value) {
      state.currentTime = value
    },
    goodsClear(state, value) {
      state.goods.length = value
    },
    goodsCheange(state, value) {
      value.forEach((item) => {
        state.goods.splice(item, 1)
      })
    },
  },
  actions: {
    async reqLyric(content, payload) {
      let result = await api.getlyric(payload.id)
      content.commit('setLyric', result.data.lrc.lyric)
    },
  },
  modules: {
  }
})
